﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZHDJ.Data.Models.MassLine
{
    public class PartyBranchDraft: EntityBase
    {
        #region 字段属性

        /// <summary>
        /// Title
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 草稿类别
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// ImageUrl
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// Content
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 发布范围
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// CreatedBy
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// CreatedTime
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// ModifyBy
        /// </summary>
        public string ModifyBy { get; set; }

        /// <summary>
        /// ModifyTime
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 是否同步到所有组织
        /// </summary>
        public bool IsSynOrgAll { get; set; }

        /// <summary>
        /// 是否提交
        /// </summary>
        public bool IsSubmit { get; set; }

        /// <summary>
        /// 专题
        /// </summary>
        public string Thematic { get; set; }
        #endregion

        #region 
        public static DataSet GetPartyBranchDraft(int pageIndex, int pageSize, string title, string branchId, string category)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };
            StringBuilder sbsql = new StringBuilder();
            sbsql.Append(@"SELECT SQL_CALC_FOUND_ROWS  DATA.*,member.UserName,branch.BranchName,IF(data.STATUS=1,'是','否') as StatusName,
                           CASE category WHEN 'systembuildplan' THEN '制度建设方案' WHEN 'givenreformplan' THEN '指定整改方案' WHEN 'specialcontrolplan' THEN '专项整治方案' END as CategoryName 
                           FROM tblpartybranchdraft data
                           LEFT JOIN tblpartyorgmember member on Data.CreateBy=member.LoginID
                           LEFT JOIN tblpartyorgbranch branch ON Data.BranchID=branch.BranchID where 1=1 ");
            if (!string.IsNullOrEmpty(title))
            {
                sbsql.Append(" and data.Title like @Title ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "Title",
                        Value = string.Format("%{0}%", title)
                    });
            }
            if (!string.IsNullOrEmpty(branchId))
            {
                sbsql.Append(" and data.BranchID=?BranchID ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "BranchID",
                        Value = branchId
                    });
            }
            if (!string.IsNullOrEmpty(category))
            {
                sbsql.Append(" and data.category=?category ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "category",
                        Value = category
                    });
            }
            sbsql.Append(" order by data.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            DataSet ds = MySqlHelper.ExecuteDataSet(sbsql.ToString(), parameters.ToArray());
            return ds;
        }

        /// <summary>
        /// 获取所有附件
        /// </summary>
        /// <param name="photoWallId"></param>
        /// <returns></returns>
        public static DataTable GetFileData(string id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select draft.ID,file.Title,file.Description,draft.CreateTime from tblpartybranchdraft draft
left join tblpartyfile file on draft.imageUrl=file.ID
where PhotoWallID=?PhotoWallID order by draft.CreateTime");

            MySqlParameter[] parameters = new MySqlParameter[]
            {
                new MySqlParameter("?PhotoWallID",MySqlDbType.VarChar)
            };
            parameters[0].Value = id;

            return MySqlHelper.ExecuteDataTable(sb.ToString(), parameters);
        }
        #endregion
    }
}
